﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections.Generic;
public partial class Main_Inventory_PurchasedReceived : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["action"] != null)
        {
            if (Request.QueryString["action"].ToString() == "addnew")
            {
                PnlAddNew.Visible = true;
                if (!IsPostBack)
                {
                    //bindPurchaseOrder();
                }
            }
            else if (Request.QueryString["action"].ToString() == "view")
            {
                if (!IsPostBack)
                {
                    PnlView.Visible = true;
                    bindPurchaseDetailsDisplay();
                }
            }
        }

        if (Request.QueryString["Msg"] != null)
        {
            lblMsg.Text = "Please Create Purchase Invoice Format";
        }
    }
    private void bindPurchaseDetailsDisplay()
    {
        DataTable dtview = DU.getdatausingprocedure("usp_IVR_GetPurchaseReceived");
        if (dtview.Rows.Count > 0)
        {
            GrdpurView.DataSource = dtview;
            GrdpurView.DataBind();
        }
    }
    //private void bindPurchaseOrder()
    //{
    //    //string sql_select = "select POID,PoPrefix,PoSuffix,PoVoucherNo from ACC_POMAIN where status=2";
    //    //string sql_select = "select convert(varchar,POID)+'-'+convert(varchar,currency) as poid,PoPrefix,PoSuffix,PoVoucherNo from ACC_POMAIN where stages=4 order by substring(povoucherno,1,0), poprefix";
    //    string sql_select = "select convert(varchar,POID)+'-'+convert(varchar,currency) as poid,PoPrefix,PoSuffix,PoVoucherNo from ACC_POMAIN where stages=4 and currency=1 order by povoucherno";
    //    //string sql_select = "select POID,PoPrefix,PoSuffix,PoVoucherNo from ACC_POMAIN where stages=4 order by substring(povoucherno,1,0), poprefix";
    //    DataTable DT = DU.getdata(sql_select);
    //    DataTable DTDisplay = new DataTable();
    //    if (DT.Rows.Count > 0)
    //    {
    //        DTDisplay.Columns.Add("POID", typeof(System.String));
    //        DTDisplay.Columns.Add("PONo", typeof(System.String));
    //        for (int i = 0; i < DT.Rows.Count; i++)
    //        {
    //            string vouchermain = "";
    //            string prefix = "";
    //            string vono = "";
    //            string suffix = "";

    //            DataRow DRow = DTDisplay.NewRow();
    //            DRow["POID"] = DT.Rows[i]["POID"];
    //            if (DT.Rows[i]["PoPrefix"].ToString() != string.Empty)
    //            {
    //                prefix = DT.Rows[i]["PoPrefix"].ToString() + "/";
    //            }
    //            if (DT.Rows[i]["PoVoucherNo"].ToString() != string.Empty)
    //            {
    //                vono = DT.Rows[i]["PoVoucherNo"].ToString();
    //            }
    //            if (DT.Rows[i]["PoSuffix"].ToString() != string.Empty)
    //            {
    //                suffix = "/" + DT.Rows[i]["PoSuffix"].ToString();
    //            }
    //            vouchermain = prefix + vono + suffix;
    //            DRow["PONo"] = vouchermain;                
    //            DTDisplay.Rows.Add(DRow);                
    //        }
    //        DdlApproved.DataSource = DTDisplay;
    //        DdlApproved.DataValueField = "poid";
    //        DdlApproved.DataTextField = "PONo";
    //        DdlApproved.DataBind();            
    //        DdlApproved.Items.Insert(0, new ListItem("- Select Purchased Order No -", "0")); 
    //    }
    //}
    protected void DdlApproved_SelectedIndexChanged(object sender, EventArgs e)
    {
        string[] poid = { };
        for (int i = 1; i < DdlApproved.SelectedValue.Length; i++)
        {
            poid = DdlApproved.SelectedValue.Split('-');
        }
        ////Response.Write(DdlApproved.SelectedValue);
        Response.Redirect("../MainPurchaseInvoice.aspx?Poid=" + poid[0] + "&Currency=" + poid[1]);
    }
    protected void GrdpurView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string poid = GrdpurView.DataKeys[e.Row.RowIndex].Values[0].ToString();
            string transferedid = GrdpurView.DataKeys[e.Row.RowIndex].Values[1].ToString();
            string trnid = GrdpurView.DataKeys[e.Row.RowIndex].Values[2].ToString();
            ((LinkButton)e.Row.Cells[8].Controls[1]).Attributes.Add("onclick", "open_Purchase_Recive_Details('" + poid + "','" + transferedid + "','" + trnid + "')");
        }
    }
    protected void GrdpurView_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.Pager)
        {
            if (e.Row.RowState == DataControlRowState.Alternate)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridview_alternate'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
            else if (e.Row.RowState == DataControlRowState.Normal)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridviewitem'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
        }
    }
    protected void GrdpurView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GrdpurView.PageIndex = e.NewPageIndex;
        bindPurchaseDetailsDisplay();
    }
    protected void rblCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        potd.Style["display"] = "block";
        DataTable DTDisplay = new DataTable();
        if (rblCountry.Items[0].Selected == true)//domestic
        {
            string sqlDomestic = "select convert(varchar,POID)+'-'+convert(varchar,currency) as poid,PoPrefix,PoSuffix,PoVoucherNo from ACC_POMAIN where stages=4 and currency=1 and Shortclose=0 order by povoucherno";
            DataTable DT = DU.getdata(sqlDomestic);            
            if (DT.Rows.Count > 0)
            {
                DTDisplay.Columns.Add("POID", typeof(System.String));
                DTDisplay.Columns.Add("PONo", typeof(System.String));
                for (int i = 0; i < DT.Rows.Count; i++)
                {
                    string vouchermain = "";
                    string prefix = "";
                    string vono = "";
                    string suffix = "";
                    DataRow DRow = DTDisplay.NewRow();
                    DRow["POID"] = DT.Rows[i]["POID"];
                    if (DT.Rows[i]["PoPrefix"].ToString() != string.Empty)
                    {
                        prefix = DT.Rows[i]["PoPrefix"].ToString() + "/";
                    }
                    if (DT.Rows[i]["PoVoucherNo"].ToString() != string.Empty)
                    {
                        vono = DT.Rows[i]["PoVoucherNo"].ToString();
                    }
                    if (DT.Rows[i]["PoSuffix"].ToString() != string.Empty)
                    {
                        suffix = "/" + DT.Rows[i]["PoSuffix"].ToString();
                    }
                    vouchermain = prefix + vono + suffix;
                    DRow["PONo"] = vouchermain;
                    DTDisplay.Rows.Add(DRow);
                }
            }
        }
        if (rblCountry.Items[1].Selected == true)//international
        {
            string sqlInternational = "select convert(varchar,POID)+'-'+convert(varchar,currency) as poid,PoPrefix,PoSuffix,PoVoucherNo from ACC_POMAIN where stages=4 and currency>1 and Shortclose=0 order by povoucherno";
            DataTable DT = DU.getdata(sqlInternational);            
            if (DT.Rows.Count > 0)
            {
                DTDisplay.Columns.Add("POID", typeof(System.String));
                DTDisplay.Columns.Add("PONo", typeof(System.String));
                for (int i = 0; i < DT.Rows.Count; i++)
                {
                    string vouchermain = "";
                    string prefix = "";
                    string vono = "";
                    string suffix = "";

                    DataRow DRow = DTDisplay.NewRow();
                    DRow["POID"] = DT.Rows[i]["POID"];
                    if (DT.Rows[i]["PoPrefix"].ToString() != string.Empty)
                    {
                        prefix = DT.Rows[i]["PoPrefix"].ToString() + "/";
                    }
                    if (DT.Rows[i]["PoVoucherNo"].ToString() != string.Empty)
                    {
                        vono = DT.Rows[i]["PoVoucherNo"].ToString();
                    }
                    if (DT.Rows[i]["PoSuffix"].ToString() != string.Empty)
                    {
                        suffix = "/" + DT.Rows[i]["PoSuffix"].ToString();
                    }
                    vouchermain = prefix + vono + suffix;
                    DRow["PONo"] = vouchermain;
                    DTDisplay.Rows.Add(DRow);
                }
            }
        }
        DdlApproved.DataSource = DTDisplay;
        DdlApproved.DataValueField = "poid";
        DdlApproved.DataTextField = "PONo";
        DdlApproved.DataBind();
        DdlApproved.Items.Insert(0, new ListItem("- Select Purchased Order No -", "0"));
    }
}
